#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>


union semun
{
	int unvalue;
};

int sem_init(int semid,int semnum,int val)
{
	union semun myval;
	myval.unvalue=val;
	semctl(semid,semnum,SETVAL,myval);

	return 0;
}

int sem_p(int semid ,int semnum)
{
	struct sembuf myctrl;
	myctrl.sem_num=semnum;
	myctrl.sem_op=-1;
	myctrl.sem_flg=0;

	semop(semid,&myctrl,1);
	return 0;
}

int sem_v(int semid ,int semnum)
{
	struct sembuf myctrl;
	myctrl.sem_num=semnum;
	myctrl.sem_op=1;
	myctrl.sem_flg=0;

	semop(semid,&myctrl,1);
	return 0;
}


int main(int argc, const char *argv[])
{
	key_t key1=ftok("../messagequeue/messagequeue1.c",'a');
	key_t key2=ftok("../messagequeue/messagequeue2.c",'b');

	int shmid=shmget(key1,4096,IPC_CREAT|0664);
	int semid=semget(key2,2,IPC_CREAT|0664);
	
	char *buf=(char *)shmat(shmid,NULL,0);

	sem_init(semid,0,1);
	sem_init(semid,1,0);


	
	while(1)
	{
		sem_p(semid,0);
		printf(">>plz input\n");
		scanf("%s",buf);
		sem_v(semid,1);
	}

	return 0;
}
